function POgetMyShipStats (callback) {
    // Reads the ship stats from the Ship Overview screen and calls the
    // given callback with the following parameters:
    // status (true=OK, false=not OK), hull, hullMax, armor, armorMax, armorStrength, shield, shieldMax, cargo, cargoMax, fuel, fuelMax
    var request = new XMLHttpRequest();
    request.onreadystatechange = function(){
        if (request.readyState != 4) return;
        if (request.status != 200) {
            callback (false);
            return;
        }
        POreadShipStats (request.responseText, callback)
    };
    request.open("get", "overview_ship.php", true);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.send();
}

function POreadShipStats (resp, callback) {
    var hull=0, hullMax=0, armor=0, armorMax=0, armorStrength=0, shield=0, shieldMax=0, cargo=0, cargoMax=0, fuel=0, fuelMax=0;
    
    // Read Cargo
    var txt = resp.split ("Cargo capacity:");
    resp = txt[1];
    txt = resp.split("<td>")[1].split("</td>")[0];
    txt = txt.split("t");
    cargo = Number(txt[0]);
    cargoMax = Number(txt[1].substr (3));
    
    // Read the Armor strength
    var txt = resp.split ("Strength x");
    if (txt.length != 1) {
        resp = txt[1];
        armorStrength = Number(txt[1][0]);
    }
    
    // Read the Armor points info
    txt = resp.split ("Armor Points:");
    resp = txt[1];
    txt = resp.split("<td>")[1].split("</td>")[0].split(" / ");
    armor = Number (txt[0]);
    armorMax = Number (txt[1]);
    
    // Read the Shield points 
    txt = resp.split ("Shield Points:");
    resp = txt[1];
    txt = resp.split("<td>")[1].split("</td>")[0].split(" / ");
    if (txt[0].indexOf ("No shield installed") == -1) {
        shield = Number (txt[0]);
        shieldMax = Number (txt[1]);
    }
    
    // Read the Hull points info
    txt = resp.split ("Hull Points:");
    resp = txt[1];
    txt = resp.split("<td>")[1].split("</td>")[0].split(" / ");
    hull = Number (txt[0]);
    hullMax = Number (txt[1]);
    
    // Read the Tank size
    txt = resp.split ("Tank Size ");
    resp = txt[1];
    fuelMax = Number(resp.split("<")[0]);
    
    // Read the current fuel in tank
    txt = resp.split ("Fuel left:")[1];
    txt = txt.split("<td>")[1].split(" <a")[0];
    fuel = Number(txt);
    
    callback (true, hull, hullMax, armor, armorMax, armorStrength, shield, shieldMax, cargo, cargoMax, fuel, fuelMax);
}

function POgetBotsToRepair (armorStrength, armor, maxArmor) {
    var step = 180/armorStrength;
    if (armor < (maxArmor - 5)) {
        return Math.ceil((maxArmor - armor) / step);
    }
    return 0;
}

function POgetMyShipCargo (callback) {
    // Reads the ship cargo from the Drop Cargo screen and calls the
    // given callback with the following parameters:
    // status (true=OK, false=not OK), fuel, bots, drugs
    var request = new XMLHttpRequest();
    request.onreadystatechange = function(){
        if (request.readyState != 4) return;
        if (request.status != 200) {
            callback (false);
            return;
        }
        POreadMyShipCargo (request.responseText, callback)
    };
    request.open("get", "drop_cargo.php", true);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.send();
}

function POreadMyShipCargo (resp, callback) {
    var fuel=0, bots=0, drugs=0;
//POlog (callback); return;
    var txt = resp.split ("Robots");
    if (txt.length > 1) {
        txt = txt[1].split(");\">")[1].split("</a>")[0];
        bots = Number(txt);
    }
    
    var txt = resp.split ("Hydrogen fuel");
    if (txt.length > 1) {
        txt = txt[1].split(");\">")[1].split("</a>")[0];
        fuel = Number(txt);
    }
    
    var txt = resp.split ("Drugs");
    if (txt.length > 1) {
        txt = txt[1].split(");\">")[1].split("</a>")[0];
        drugs = Number(txt);
    }
    
    callback (true, fuel, bots, drugs);
}

function POuseResource (id, amount, callback) {
    var request = new XMLHttpRequest();
    request.onreadystatechange = function(){
        if (request.readyState != 4) return;
        if (request.status != 200) callback (false);
        else callback (true);
    };
    request.open("get", "main.php?amount="+amount+"&resid="+id+"&useres=Use", true);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.send();
	
	// Druggy changes
	if (top.useDruggy) 
	{
		if (id == '51') druggySave(amount);
	}
}

function POgetOtherShipInfo (pilotID, callback) {
    // Reads the Hull, armor, shields and Ship info about the given pilot
    // (the Combat screen has to be available) and calls the
    // given callback with the following parameters:
    // status (true=OK, false=not OK), pilotID, ship, hull, armor, shield
    var request = new XMLHttpRequest();
    request.onreadystatechange = function(){
        if (request.readyState != 4) return;
        if (request.status != 200) {
            callback (false);
            return;
        }
        POreadOtherShipInfo (request.responseText, callback, pilotID)
    };
    request.open("get", "ship2ship_combat.php?playerid="+pilotID, true);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.send();
}

function POreadOtherShipInfo (resp, callback, pilotID) {
    var hull=0, armor=0, shield=0, ship="";
    
    // Hull
    var txt = resp.split (">Hull points<")[1];
    if (!txt) {
        callback (false, pilotID);
        return;
    }
    txt = txt.split("width=\"")[1].split ("\"")[0];
    hull = Number (txt)*2;
    
    // Armor
    var txt = resp.split (">Armor points<")[1];
    txt = txt.split("width=\"")[1].split ("\"")[0];
    armor = Number (txt)*2;
    
    // Shield
    var txt = resp.split (">Shield points<");
    if (txt.length>1) {
        
        txt = txt[1].split("width=\"")[1].split ("\"")[0];
        armor = Number (txt)*2;
    }
    
    // Ship
    var txt = resp.split ("ships/")[2];
    ship = txt.split(".")[0];
    var paint = ship.indexOf ("_paint");
    if (paint != -1) {
        ship = ship.substr (0, paint);
    }
    
    callback (true, pilotID, ship, hull, armor, shield);
}
